package cn.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import cn.dao.JDBCUtils;
import cn.dao.LoginDao;
import cn.dao.impl.LoginDaoImpl;
import cn.pojo.Login;
import cn.service.LoginService;

public class LoginServiceImpl implements LoginService {
	private LoginDao loginDao = new LoginDaoImpl();
	@Override
	public Login getUserByEmailAndPassword(String email, String password){
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			//关闭事物默认的自动提交
			connection.setAutoCommit(false);
			Login login = loginDao.getUserByEmailAndPassword(connection, email, password);
			connection.commit();//手动提交
			return login;
		} catch (ClassNotFoundException|SQLException e) {
			if(connection!=null) {
				//事物回滚
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally {
			JDBCUtils.close(connection);//关闭连接
		}
		return null;
	}

}
